/**
 * 初始化商品录入管理详情对话框
 */
var ManagerGoodsInfoDlg = {
    managerGoodsInfoData : {},
    zTreeInstance:null,
    //goodsCategoryId 这个属性用户点击树节点的时候存储下来
    tempGoodsCategoryId:null
};

/**
 * 清除数据
 */
ManagerGoodsInfoDlg.clearData = function() {
    this.managerGoodsInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ManagerGoodsInfoDlg.set = function(key, val) {
    this.managerGoodsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ManagerGoodsInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ManagerGoodsInfoDlg.close = function() {
    parent.layer.close(window.parent.ManagerGoods.layerIndex);
}

/**
 * 收集数据
 */
ManagerGoodsInfoDlg.collectData = function() {
    this
    .set('id')
    .set('goodsCode')
    .set('goodsName')
    .set('goodsModel')
    .set('goodsUnit')
    .set('goodsPurchasePrice')
    .set('goodsSellingPrice')
    .set('goodsInventoryQuantity')
    .set('goodsMinNum')
    .set('goodsLastPurchasePrice')
    .set('createBy')
    .set('createDate')
    .set('updateBy')
    .set('updateDate')
    .set('remarks')
    .set('delFlag')
    .set('supplierId')
    .set('goodsCategoryId');
}

/**
 * 提交添加
 */
ManagerGoodsInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/managerGoods/add", function(data){
        Feng.success("添加成功!");
        window.parent.ManagerGoods.table.refresh();
        ManagerGoodsInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });

    this.managerGoodsInfoData['goodsCategoryId'] = this.tempGoodsCategoryId;
    ajax.set(this.managerGoodsInfoData);
    console.log('调用ManagerGoodsInfoDlg.addSubmit传输的数据:'+JSON.stringify(ajax.data));
    ajax.start();
}

/**
 * 提交修改
 */
ManagerGoodsInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/managerGoods/update", function(data){
        Feng.success("修改成功!");
        window.parent.ManagerGoods.table.refresh();
        ManagerGoodsInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    this.managerGoodsInfoData['goodsCategoryId'] = $("#categoryId").val();
    console.log('商品分类id=',$("#categoryId").val());

    ajax.set(this.managerGoodsInfoData);
    console.log('调用ManagerGoodsInfoDlg.editSubmit传输的数据:'+JSON.stringify(ajax.data));
    ajax.start();
}

ManagerGoodsInfoDlg.selectSupplier = function(){
    //查找供应商下拉框
    var ajax = new $ax(Feng.ctxPath + "/managerSupplier/list", function (data) {
        $("#supplierId").empty();
        $.each(data, function (index, supplier) {
            var supplierId = supplier['id'];
            var supplierName = supplier['supplierName'];
            $("#supplierId").append("<option value='"+supplierId+"'>"+supplierName+"</option>");
        })
    },function(data){
        Feng.error("获取供应商失败!" + data.responseJOSN.message + "!");
    });
    ajax.start();
}

ManagerGoodsInfoDlg.selectGoodsUnit = function(){
    //查找计量单位下拉框
    var ajax = new $ax(Feng.ctxPath + "/goodsUnit/list", function (data) {
        $("#goodsUnit").empty();
        $.each(data, function (index, goodsUnit) {
            var id = goodsUnit['id'];
            var unit = goodsUnit['unit'];
            $("#goodsUnit").append("<option value='"+unit+"'>"+unit+"</option>");
        });
    },function(data){
        Feng.error("获取供应商失败!" + data.responseJOSN.message + "!");
    });
    ajax.start();
}



/**
 * 点击商品ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
ManagerGoodsInfoDlg.onClickCate = function (e, treeId, treeNode) {
    $("#pName").attr("value", ManagerGoodsInfoDlg.zTreeInstance.getSelectedVal());
    $("#parentId").attr("value", treeNode.id);
    ManagerGoodsInfoDlg.tempGoodsCategoryId = treeNode.id;
    console.log('点击了树节点ID：'+ManagerGoodsInfoDlg.tempGoodsCategoryId,'点击了树节点名称:'+ ManagerGoodsInfoDlg.zTreeInstance.getSelectedVal());
}

/**
 * 显示商品分类的树
 *
 * @returns
 */
ManagerGoodsInfoDlg.showGoodsCateSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentCateMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏商品分类的树
 */
ManagerGoodsInfoDlg.hideGoodsCateSelectTree = function () {
    $("#parentCateMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentCateMenu" || $(
        event.target).parents("#parentCateMenu").length > 0)) {
        ManagerGoodsInfoDlg.hideGoodsCateSelectTree();
    }
}


$(function() {

    //注册供应商下拉框事件
    $("#supplierId").on('click', function () {
        ManagerGoodsInfoDlg.selectSupplier();
    })
    //商品单位下拉框
    $("#goodsUnit").on('click',function () {
        ManagerGoodsInfoDlg.selectGoodsUnit();
    })

    var ztree = new $ZTree("parentCateMenuTree", "/goodsCategory/tree");
    ztree.bindOnClick(ManagerGoodsInfoDlg.onClickCate);
    ztree.init();
    ManagerGoodsInfoDlg.zTreeInstance = ztree;

});
